//
//  GX_TabBar.m
//  07-CustomizeTabBarController
//
//  Created by 曹曹 on 16/7/25.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "GX_TabBar.h"
#import "MiddleViewController.h"

@interface GX_TabBar ()
@property (nonatomic, weak) UIButton *middleButton;
@end

@implementation GX_TabBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"]
                          forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"]
                          forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        self.middleButton = button;
    }
    return self;
}

- (void)button:(UIButton *)button {
    NSLog(@"modal");
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.middleButton.bounds = CGRectMake(0, 0, self.middleButton.currentBackgroundImage.size.width, self.middleButton.currentBackgroundImage.size.height);
    self.middleButton.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
    // 设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = self.frame.size.width / 5;
    CGFloat buttonH = self.frame.size.height;
    NSInteger index = 0;
    for (UIView *button in self.subviews) {
        if (![button isKindOfClass:[UIControl class]] || button == self.middleButton) continue;
        // 计算按钮的x值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        // 增加索引
        index++;
    }
}

@end
